function validator (err, rule, callback, cb) {
    cb(rule.field, err)
    err ? callback(new Error(err)) : callback()
}

/**
 *  获取舱单基本信息校验规则
 * @param {Function} cb 规则校验后的回调
 * @returns {Object} 校验规则
 */
export function getManifestRules (cb) {
    return {
        cargoBatchNo: [
            {
                required: true,
                validator: function (rule, value, callback) {
                    const err = value ? '' : '请输入货物运输批次号'
                    validator(err, rule, callback, cb)
                },
                trigger: 'change'
            }
        ],
        iePort: [
            {
                required: true,
                validator: function (rule, value, callback) {
                    const err = value ? '' : '请选择进出境口岸海关'
                    validator(err, rule, callback, cb)
                },
                trigger: 'change'
            }
        ],
        loadingDate: [
            {
                required: true,
                validator: function (rule, value, callback) {
                    const err = value ? '' : '请设置货物装载时间'
                    validator(err, rule, callback, cb)
                },
                trigger: 'change'
            }
        ],
        unloadingPlace: [
            {
                required: true,
                validator: function (rule, value, callback) {
                    const err = value ? '' : '请选择卸货地代码'
                    validator(err, rule, callback, cb)
                },
                trigger: 'change'
            }
        ],
        loadingPlace: [
            {
                required: true,
                validator: function (rule, value, callback) {
                    const err = value ? '' : '请选择装货地代码'
                    validator(err, rule, callback, cb)
                },
                trigger: 'change'
            }
        ],
        representativePerson: [
            {
                required: true,
                validator: function (rule, value, callback) {
                    const err = value ? '' : '请补全舱单传输人信息'
                    validator(err, rule, callback, cb)
                },
                trigger: 'change'
            }
        ],
        agentName: [
            {
                required: true,
                validator: function (rule, value, callback) {
                    const err = value ? '' : '请填写申报单位'
                    validator(err, rule, callback, cb)
                },
                trigger: 'blur,change'
            }
        ]
    }
}

export function getManifestErrorForm () {
    return {
        cargoBatchNo: '',
        iePort: '',
        loadingDate: '',
        unloadingPlace: '',
        loadingPlace: '',
        representativePerson: '',
        agentName: ''
    }
}

/**
 *  获取提运单表单验证规则
 * @param {Function} cb 规则校验后的回调
 * @returns {Object} 校验规则
 */
export function getConsignmentRules (cb) {
    return {
        billNo: [
            {
                required: true,
                validator: function (rule, value, callback) {
                    const err = value ? '' : '请输入提（运）单号'
                    validator(err, rule, callback, cb)
                },
                trigger: 'change'
            }
        ],
        cargoClearanceCode: [
            {
                required: true,
                validator: function (rule, value, callback) {
                    const err = value ? '' : '请输入海关货物通关代码'
                    validator(err, rule, callback, cb)
                },
                trigger: 'change'
            }
        ],
        totalPackageQuantity: [
            {
                required: true,
                validator: function (rule, value, callback) {
                    const err = value || value === 0
                        ? /^\d+(\.{0,1}\d+){0,1}$/.test(value)
                            ? ''
                            : '请输入正数'
                        : '请输入货物总件数'
                    validator(err, rule, callback, cb)
                },
                trigger: 'change'
            }
        ],
        packingType: [
            {
                required: true,
                validator: function (rule, value, callback) {
                    const err = value ? '' : '请选择包装种类'
                    validator(err, rule, callback, cb)
                },
                trigger: 'change'
            }
        ],
        totalGrossMeasure: [
            {
                required: true,
                validator: function (rule, value, callback) {
                    const err = value || value === 0
                        ? /^\d+(\.{0,1}\d+){0,1}$/.test(value)
                            ? ''
                            : '请输入正数'
                        : '请输入货物总毛重'
                    validator(err, rule, callback, cb)
                },
                trigger: 'change'
            }
        ],
        cargoValue: [
            {
                required: true,
                validator: function (rule, value, callback) {
                    const err = value || value === 0
                        ? /^\d+(\.{0,1}\d+){0,1}$/.test(value)
                            ? ''
                            : '请输入正数'
                        : '请输入货物价值'
                    validator(err, rule, callback, cb)
                },
                trigger: 'change'
            }
        ],
        amountType: [
            {
                required: true,
                validator: function (rule, value, callback) {
                    const err = value ? '' : '请选择金额类型'
                    validator(err, rule, callback, cb)
                },
                trigger: 'change'
            }
        ],
        consigneeName: [
            {
                required: true,
                validator: function (rule, value, callback) {
                    const err = value ? '' : '请填写收货人'
                    validator(err, rule, callback, cb)
                },
                trigger: 'blur,change'
            }
        ],
        consignorName: [
            {
                required: true,
                validator: function (rule, value, callback) {
                    const err = value ? '' : '请填写发货人'
                    validator(err, rule, callback, cb)
                },
                trigger: 'blur,change'
            }
        ]
    }
}

export function getConsignmentErrorForm () {
    return {
        billNo: '',
        cargoClearanceCode: '',
        totalPackageQuantity: '',
        packingType: '',
        totalGrossMeasure: '',
        cargoValue: '',
        amountType: '',
        consigneeName: '',
        consignorName: ''
    }
}
